package com.runa.monitor.platinterface.mqtt;


import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.comm.entity.CommNetEquipment;
import com.runa.persistence.SerializeUtil;
import com.runa.persistence.service.LogService;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.util.FaultUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MqttUtil {

    public static String topicOfNoneContent = "comm/net/resp";
    public static String topicOfNoneContentForNode = "comm/node/resp";
    public static String successString = "成功";

    public static void sendOnLineStateOfNoneContent(CommNetEquipment commNetEquipment, boolean online) {
        AsynResult result = new AsynResult(online ? 800 : 900, online ? "上线" : "下线"
                , commNetEquipment.getId(), null, null, System.currentTimeMillis()
                , null, commNetEquipment.getSerial());
        log.debug("网络设备状态变更：{}", result);
        MqttBaseController.getConnector().sendMessage("comm/net/onoffline", SerializeUtil.obj2Bytes(result));
    }

    public static void sendOnLineStateOfNoneContent(String id, String serial, boolean online) {//offLine onLine
        AsynResult result = new AsynResult(online ? 800 : 900, online ? "上线" : "下线"
                , id, null, null, System.currentTimeMillis()
                , null, serial);
        log.debug("网络设备状态变更：{}", result);
        MqttBaseController.getConnector().sendMessage("comm/net/onoffline", SerializeUtil.obj2Bytes(result));
    }

    public static void send(String topic, AsynResult asynResult) {
        LogService.result(asynResult);
        FaultUtil.analysis(topic, asynResult);
        LogUtil.info(asynResult.getId(), JSON.toJSONString(asynResult));
        MqttBaseController.getConnector().sendMessage(topic, asynResult);
    }

    public static void sendOfWithoutArchive(String taskNo, String holdInfo, String topic, String identity) {
        AsynResult asynResult = AsynResult.builder().identity(identity)
                .task(taskNo).holdInfo(holdInfo).build();
        asynResult.noSuchFileSetting();
        log.debug("TOPIC:{}------AsynResult:{}", topic, asynResult);
        MqttBaseController.getConnector().sendMessage(topic, SerializeUtil.obj2Bytes(asynResult));
    }


    //调用处自行打印日志
    public static void send(AsynResult asynResult) {
        MqttBaseController.getConnector().sendMessageForService(asynResult);
    }


    public static void send(String topic, byte[] data) {
        MqttBaseController.getConnector().sendMessage(topic, data);
    }

}
